package com.knockim.view;

import java.lang.reflect.Field;

import com.knockim.R;
import com.knockim.R.layout;
import com.knockim.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity implements View.OnClickListener, TabHost.OnTabChangeListener {

	/**
     * 显示于底部的Tab
     */
	private TabHost mTabHost;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initTab();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabChanged(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 初始化Tab相关View
	 */
	private void initTab() {
		
		mTabHost = getTabHost();
		mTabHost.setOnTabChangedListener(this);
		
		TabHost.TabSpec vicinityTabSpec = mTabHost.newTabSpec("")
										   .setIndicator(initTabIndicator(R.string.tab_label_vicinity, R.drawable.main_tab_vicinity))
										   .setContent(new Intent(this, MainTabVicinityActivity.class));
		TabHost.TabSpec favoriteTabSpec = mTabHost.newTabSpec("")
										   .setIndicator(initTabIndicator(R.string.tab_label_favorite, R.drawable.main_tab_favorite))
										   .setContent(new Intent(this, MainTabFavoriteActivity.class));
		TabHost.TabSpec settingTabSpec = mTabHost.newTabSpec("")
										   .setIndicator(initTabIndicator(R.string.tab_label_setting, R.drawable.main_tab_setting))
										   .setContent(new Intent(this, MainTabSettingActivity.class));
		
		mTabHost.addTab(vicinityTabSpec);
		mTabHost.addTab(favoriteTabSpec);
		mTabHost.addTab(settingTabSpec);
	}
	
	/**
     * 初始化TabSpec的标签View<BR>
     * 
     * @param labelResId Tab标签
     * @param iconResId Tab图标
     * @return 显示于底部Tab的View
     */
    private View initTabIndicator(int labelResId, int iconResId) {
        View indicator = getLayoutInflater().inflate(R.layout.main_tab_view, null);
        if(labelResId == 0) {
        	indicator.findViewById(R.id.tab_btn_view).setVisibility(View.GONE);
        } else {
        	((TextView) indicator.findViewById(R.id.tab_btn_view)).setText(labelResId);
        }
        ((ImageView) indicator.findViewById(R.id.tab_imagebtn_view)).setImageResource(iconResId);
        	
        return indicator;
    }

}
